﻿using System.Windows.Forms;

namespace CSharpHelpers.Winforms
{
    class EventHelper
    {
        /// <summary>
        /// 控制只能輸入 數字
        /// </summary>
        /// <param name="e">KeyPress 事件參數</param>
        public static void IsInputNumber(object sender, KeyPressEventArgs e)
        {
            e.Handled = true; // true 不接受事件
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (Keys)(e.KeyChar) == Keys.Back)
            {
                e.Handled = false;
                return;
            }
        }

        /// <summary>
        /// 驗證數字與小數點
        /// </summary>
        public static void IsInputDouble(object sender, KeyPressEventArgs e)
        {
            //if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
            //{
            //    e.Handled = true;
            //}
            //else if (Char.IsPunctuation(e.KeyChar))
            //{
            //    if (((TextBox)sender).Text.LastIndexOf('.') != -1)
            //    {
            //        e.Handled = true;
            //    }
            //}
            //else
            //{
            //    e.Handled = false;
            //}

            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                        e.Handled = true;
                }
                else
                    e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        /// <summary>
        /// 控制只能輸入 數字 字母
        /// </summary>
        /// <param name="e">KeyPress 事件參數</param>
        public static void IsInputNumberLetter(object sender, KeyPressEventArgs e)
        {
            e.Handled = true; // true 不接受事件^[a-zA-Z0-9]+$
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (Keys)(e.KeyChar) == Keys.Back || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z'))
            {
                e.Handled = false;
                return;

            }
        }

        /// <summary>
        /// 驗證數字與小數點
        /// </summary>
        public static void VerifyThatTheDigital(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                        e.Handled = true;
                }
                else
                    e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }

            #region
            //if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
            //{
            //    e.Handled = true;
            //}
            //else if (Char.IsPunctuation(e.KeyChar))
            //{
            //    if (((TextBox)sender).Text.LastIndexOf('.') != -1)
            //    {
            //        e.Handled = true;
            //    }
            //}
            //else
            //{
            //    e.Handled = false;
            //}
            #endregion
        }

        /// <summary>
        /// 不能輸入中文
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void NotEnterChinese(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '8') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z' || e.KeyChar == '\b'))
                e.Handled = false;
            else
                e.Handled = true;
        }
    }
}
